package com.ihr360.shared.advice.response;

import com.ihr360.commons.vo.ErrorInfo;
import com.ihr360.commons.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.io.File;
import java.lang.reflect.Method;

/**
 * @author : David
 */
@ControllerAdvice
public class Ihr360ResponseAdvice implements ResponseBodyAdvice<Object> {

    private final static String SPRING_BOOT = "org.springframework.boot";
    private final static String SWAGGER_DOCUMENTATION = "springfox.documentation";

    @Value("${ihr360.response.advice.disabled:false}")
    private boolean disableAdvice;

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        // application.properties中通过参数禁用
        if (disableAdvice) {
            return false;
        }
        // String类型的返回不处理
        if (StringHttpMessageConverter.class.isAssignableFrom(converterType)) {
            return false;
        }
        // ResultInfo类型的返回不处理
        if (ResultInfo.class.isAssignableFrom(returnType.getParameterType())) {
            return false;
        }

        // 不重写spring boot接口的返回结果
        if (returnType.getParameterType().getName().startsWith(SPRING_BOOT)) {
            return false;
        }
        // 不重写Swagger接口的返回结果
        if (returnType.getMethod().getDeclaringClass().getName().startsWith(SWAGGER_DOCUMENTATION)) {
            return false;
        }

        // ErrorInfo类型的返回不处理
        if (ErrorInfo.class.isAssignableFrom(returnType.getParameterType())) {
            return false;
        }

        // 判断方法注解@DisableResponseAdvice
        Method method = returnType.getMethod();
        DisableResponseAdvice disableResponseAdvice = AnnotationUtils.findAnnotation(method, DisableResponseAdvice.class);
        if (disableResponseAdvice != null) {
            return !disableResponseAdvice.disabled();
        }

        // 判断类注解@DisableResponseAdvice
        Class declaringClass = method.getDeclaringClass();
        disableResponseAdvice = AnnotationUtils.findAnnotation(declaringClass, DisableResponseAdvice.class);
        if (disableResponseAdvice != null) {
            return !disableResponseAdvice.disabled();
        }

        return true;
    }


    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body == null) {
            return null;
        }
        if (body instanceof File) {
            return body;
        }
        ResultInfo<Object> resultInfo = new ResultInfo<>();
        resultInfo.setCode(ResultInfo.OK);
        resultInfo.setData(body);
        return resultInfo;
    }
}
